<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class database extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function getData() {
        $sql = 'SELECT name FROM finance';
        $query = $this->db->query($sql);
        return $query->result();
    }

    function getNews() {
        $sql = 'SELECT * FROM news ORDER BY date DESC';
        $query = $this->db->query($sql);
        return $query->result();
    }

    function getFeedback() {
        $sql = 'SELECT * FROM feedback';
        $query = $this->db->query($sql);
        return $query->result();
    }

    function insertFeedback($name, $affiliation, $email, $category, $comment) {
        $sql = "INSERT INTO feedback (name, affiliation, email, category, comment) VALUES 
            (" . $this->db->escape($name) . ", 
             " . $this->db->escape($affiliation) . " , 
             " . $this->db->escape($email) . " , 
             " . $this->db->escape($category) . " , 
             " . $this->db->escape($comment) . ")";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return "success";
        }
        return FALSE;
    }

    function deleteFeedback($id) {
        $sql = "DELETE FROM feedback WHERE id = '$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }

    function getNewsByID($id) {
        $sql = "SELECT * FROM news WHERE id = '$id' LIMIT 1";
        $query = $this->db->query($sql);
        return $query->row();
    }

    function insertNews($date, $news) {
        $sql = "INSERT INTO news (date, news) VALUES (" . $this->db->escape($date) . ", " . $this->db->escape($news) . ")";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }

    function deleteNews($id) {
        $sql = "DELETE FROM news WHERE id = '$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }

    function updateNews($id, $news, $date) {
        $sql = "UPDATE news SET news = '$news', date = '$date' WHERE id='$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }

    function getLinks(){
        $sql = "SELECT * FROM links";
        $query = $this->db->query($sql);
        return $query;
    }
    
    function insertLink($name, $link, $header, $type, $description) {
        $sql = "INSERT INTO links (name, link, header, type, description) VALUES 
            (" . $this->db->escape($name) . ", 
             " . $this->db->escape($link) . " , 
             " . $this->db->escape($header) . " , 
             " . $this->db->escape($type) . " , 
             " . $this->db->escape($description) . ")";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return "success";
        }
        return FALSE;
    }
    
    function deleteLink($id){
        $sql = "DELETE FROM links WHERE id = '$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }
    
    function updateLink($id, $name, $header, $link, $type, $description){
        $sql = "UPDATE links SET name = '$name', header = '$header', link = '$link'
            , type = '$type', description = '$description' WHERE id='$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }
    
    function insertPaper($filename, $filepath, $date, $name, $description){
        $sql = "INSERT INTO papers (name, date, filepath, filename, description) VALUES 
            (" . $this->db->escape($name) . ", 
             " . $this->db->escape($date) . " , 
             " . $this->db->escape($filepath) . " , 
             " . $this->db->escape($filename) . " , 
             " . $this->db->escape($description) . ")";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }
    
    function getPapers(){
        $sql = "SELECT * FROM papers";
        $query = $this->db->query($sql);
        return $query;
    }
    
    function getPaperByID($id){
        $sql = "SELECT * FROM papers WHERE id = '$id' LIMIT 1";
        $query = $this->db->query($sql);
        return $query->row_array();
    }
    
    function updatePaper($id, $filename, $filepath, $date, $name, $description){
        $sql = "UPDATE papers SET name = '$name', date = '$date', description = '$description'
            , filename = '$filename', filepath = '$filepath' WHERE id='$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }
    
    function deletePaper($id){
        $sql = "DELETE FROM papers WHERE id = '$id'";
        $this->db->query($sql);
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        }
        return FALSE;
    }
}

?>
